package com.zyf.springcloud.controller;

import com.zyf.springcloud.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class APIController {
    @Autowired
    RolePermissionServiceImpl rolePermissionService;
    @Autowired
    RoleServiceImpl roleService;
    @Autowired
    PermissionServiceImpl permissionService;
    @Autowired
    RoleUserServiceImpl roleUserService;
    @Autowired
    UserServiceImpl userService;

    @PostMapping("/api/selectRoleByPermissionURL")
    public List<String> selectRoleEntitiesByPermissionURL(@RequestParam(value = "URL") String URL) {
        int PermissionId = permissionService.selectPermissionIdByPermissionURL(URL);
        List<Integer> list = rolePermissionService.selectRoleIdByPermissionId(PermissionId);
        return (List<String>) roleService.selectRoleNameByRoleId(list);
    }

    @GetMapping("/api/selectRoleNameByUserName/{username}")
    public String selectRoleNameByUserName(@PathVariable("username") String username) {
        Long UserId = userService.selectIdByUsername(username);
        int RoleId = roleUserService.selectRoleIdByUserId(UserId);
        if (RoleId==0){
            return null;
        }
        return roleService.selectRoleNameByRoleId(RoleId);
    }

}
